// Copyright 2010 Alexandre Tristan St-Cyr <tristan.stcyr@gmail.com>

package com.codeplex.loadjava;

import org.objectweb.asm.AnnotationVisitor;
import org.objectweb.asm.Attribute;
import org.objectweb.asm.Label;
import org.objectweb.asm.MethodVisitor;

import com.codeplex.loadjava.model.Field;
import com.codeplex.loadjava.model.FieldReference;
import com.codeplex.loadjava.model.JavaClass;
import com.codeplex.loadjava.model.Method;
import com.codeplex.loadjava.model.MethodInvocation;
import com.codeplex.loadjava.model.TypeReference;

class JavaModelMethodAdapter extends JavaModelReaderVisitor implements MethodVisitor {
	
	private Method method;
	
	public JavaModelMethodAdapter(
			IJavaModelVisitor entityVisitor, 
			Method method) {
		super(entityVisitor);
		this.method = method;
	}
	
	@Override
	public AnnotationVisitor visitAnnotationDefault() {
		// TODO Implement when annotation loading is added
		return null;
	}

	@Override
	public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
		// TODO Implement when annotation loading is added
		return null;
	}

	@Override
	public AnnotationVisitor visitParameterAnnotation(int parameter,
			String desc, boolean visible) {
		// TODO Implement when annotation loading is added
		return null;
	}

	@Override
	public void visitAttribute(Attribute attr) {
		// TODO Implement when attributes loading is added

	}
	
	@Override
	public void visitFieldInsn(int opcode, String owner, String name,
			String desc) {
		Field field = new Field(new JavaClass(owner), name, new TypeReference(desc));
		this.visitField(field);
		this.entityVisitor.visitFieldReference(new FieldReference(field, this.method));
	}

	@Override
	public void visitMethodInsn(
			int opcode, 
			String owner, 
			String name,
			String methodDescriptor) {
		Method callee = new Method(new JavaClass(owner), name, methodDescriptor);
		this.visitMethod(callee);
		MethodInvocation mi = new MethodInvocation(this.method, callee);
		this.entityVisitor.visitMethodInvocation(mi);
	}
	
	@Override
	public void visitCode() {}
	@Override
	public void visitFrame(int type, int nLocal, Object[] local, int nStack,
			Object[] stack) {}
	@Override
	public void visitInsn(int opcode) {}
	@Override
	public void visitIntInsn(int opcode, int operand) {}
	@Override
	public void visitVarInsn(int opcode, int var) {}
	@Override
	public void visitJumpInsn(int opcode, Label label) {}
	@Override
	public void visitLabel(Label label) {}
	@Override
	public void visitTypeInsn(int opcode, String type) {
		// TODO Implement when direct type referencing is added
	}
	@Override
	public void visitMultiANewArrayInsn(String desc, int dims) {
		// TODO Implement when direct type referencing is added
	}
	@Override
	public void visitTryCatchBlock(Label start, Label end, Label handler,
			String type) {
		// TODO Implement when direct type referencing is added
	}
	@Override
	public void visitLocalVariable(String name, String desc, String signature,
			Label start, Label end, int index) {
		// TODO Implement when local variable loading is added
	}
	@Override
	public void visitLdcInsn(Object cst) {}
	@Override
	public void visitIincInsn(int var, int increment) {}
	@Override
	public void visitTableSwitchInsn(int min, int max, Label dflt,
			Label[] labels) {}
	@Override
	public void visitLookupSwitchInsn(Label dflt, int[] keys, Label[] labels) {}
	@Override
	public void visitLineNumber(int line, Label start) {}
	@Override
	public void visitMaxs(int maxStack, int maxLocals) {}
	@Override
	public void visitEnd() {}
}
